<template>
  <div style="margin-top: 20px">
    <el-form size="mini" :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="关键字">
        <el-input v-model="searchForm.keyword" placeholder="宠物名称"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchForm.state" placeholder="审核状态">
          <el-option label="上架" value="1"></el-option>
          <el-option label="下架" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="上架时间">
        <el-date-picker
                v-model="searchForm.betweenTime"
                type="daterange"
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="toQuery">查询</el-button>
        <el-button type="info" @click="toAdd">新增</el-button>
        <!--<el-button type="primary" @click="toAdd">新增</el-button>-->
        <el-button type="danger" @click="">批量删除</el-button>
        <el-button type="warning" @click="upOrDownPet(1)">上架</el-button>
        <el-button type="default" @click="upOrDownPet(0)">下架</el-button>
      </el-form-item>
    </el-form>
    <el-table
            :data="tableData"
            border
            style="width: 100%;"
            @selection-change="handleSelectionChange">
      <el-table-column
              type="selection"
              width="55">
      </el-table-column>
      <el-table-column
              prop="resources"
              label="宠物图片">
        <template slot-scope="scope">
          <el-image :src="scope.row.resources"
                    :preview-src-list="[scope.row.resources]"></el-image>
        </template>
      </el-table-column>
      <el-table-column
              prop="name"
              label="宠物名称">
      </el-table-column>
      <el-table-column
              prop="state"
              label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state == 0" type="info">下架</el-tag>
          <el-tag v-if="scope.row.state == 1" type="warning">上架</el-tag>
        </template>
      </el-table-column>
      <el-table-column
              prop="age"
              label="年龄">
      </el-table-column>
      <el-table-column
              prop="saleprice"
              label="销售价格">
      </el-table-column>
      <el-table-column
              prop="costprice"
              label="成本价">
      </el-table-column>
      <el-table-column prop="petTypeName"
                       label="宠物类型">

      </el-table-column>
      <el-table-column
              prop="shopName"
              label="所属门店">
      </el-table-column>
      <el-table-column
              prop="userId"
              label="宠物发布人">
      </el-table-column>
      <el-table-column
              prop="onsaletime"
              label="上架时间">
      </el-table-column>

      <el-table-column label="操作" width="80px">
        <template slot-scope="scope">
          <el-button
                  size="mini"
                  @click="toEdit(scope.row)">编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
            style="margin-top: 20px"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="searchForm.page"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="searchForm.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>


    <el-dialog title="审核操作" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="标题" label-width="100px">
          <el-input v-model="form.title" disabled></el-input>
        </el-form-item>
        <el-form-item label="审核备注" label-width="100px">
          <el-input
                  type="textarea"
                  placeholder="请输入内容"
                  v-model="form.note"
                  maxlength="200"
                  show-word-limit
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="shenghe(1)">审核通过</el-button>
        <el-button @click="shenghe(2)">审核驳回</el-button>
      </div>
    </el-dialog>

    <el-dialog  title="编辑宠物信息" :visible.sync="dialogPetFormVisible">
      <el-form ref="form" :model="petForm" label-width="80px">
        <el-form-item label="宠物名称">
          <el-input v-model="petForm.name"></el-input>
        </el-form-item>
        <el-form-item label="发布时间">
          <el-date-picker
                  v-model="searchForm.onOffSaleTime"
                  type="daterange"
                  value-format="yyyy-MM-dd"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="宠物图片">
          <el-upload
                  class="avatar-uploader"
                  action="http://localhost:80/file/ossUpload"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload">
            <img v-if="petForm.resources" :src="petForm.resources" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="销售价">
          <el-input v-model="petForm.saleprice"></el-input>
        </el-form-item>
        <!--<el-form-item label="促销价">
          <el-input v-model="petForm.chuxiaoPrice"></el-input>
        </el-form-item>-->
        <el-form-item label="服务备注">
          <el-input type="textarea" v-model="petForm.intro"></el-input>
        </el-form-item>
        <el-form-item label="详细介绍">
          <quill-editor
                  :options="editorOption"
                  v-model="petForm.adoptNotice"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="savePet()">保存</el-button>
        <el-button @click="dialogPetFormVisible=false">取消</el-button>
      </div>
    </el-dialog>

    <el-dialog
            title="提示"
            :visible.sync="dialogVisible"
            width="30%">
      <span>你确认{{state?"上":"下"}}架&emsp;{{names}}&emsp;吗</span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="sureUpDown()">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
  import {quillEditor} from "vue-quill-editor"; //调用编辑器
  import 'quill/dist/quill.core.css';
  import 'quill/dist/quill.snow.css';
  import 'quill/dist/quill.bubble.css';

  import quillConfig from "../../common/js/quill-config" //引入配置


  export default {
    name: "PetUpDown",
    components: { // 引入一个 自定义的components组件
      'quill-editor': quillEditor //QuillEditor  ---默认名称：quill-editor
    },
    data() {
      return {
        //数据
        editorOption: quillConfig,
        onOffSaleTime: null, //查询条件

        form: { //下面的3个属性，可省略
          id: '',//审核的数据的主键 ID
          title: '', //显示的标题
          note: '', //备注,
          state: null //审核状态
        },
        petForm: {
          //detail: {}
        },//宠物编辑表单
        dialogPetFormVisible: false, //宠物编辑弹框
        dialogFormVisible: false, //宠物审核弹框
        total: 0, //总记录数
        multipleSelection: [],
        searchForm: {
          betweenTime: null, //查询条件
          page: 1,
          size: 10
        },
        tableData: [],
        state:0,
        names:"",
        dialogVisible:false
      }
    },
    methods: {

      sureUpDown(){
        var ids = this.multipleSelection.map(item=>item.id);
        this.$http.post('/pet/upOrDownPet', {ids:ids,state:this.state}).then(res => {
          let {success, message} = res.data;
          if (success) {
            this.dialogVisible = false;
            this.toQuery();
          }
          this.$message.info(message);
        })
      },

      upOrDownPet(state){

        var namesArr = this.multipleSelection.map(item=>item.name);
        console.log(namesArr)
        this.names = namesArr.join(" , ")
        this.state = state;
        this.dialogVisible = true;
      },

      savePet() {
        if (this.searchForm.onOffSaleTime && this.searchForm.onOffSaleTime.length > 0) {//判断参数非空（!= '' && != underfind && != NAN）
          this.petForm.onsaletime = this.searchForm.onOffSaleTime[0]
          this.petForm.offsaletime = this.searchForm.onOffSaleTime[1]
        }

        this.$http.post('/pet', this.petForm).then(res => {
          let {success, message} = res.data;
          if (success) {
            this.dialogPetFormVisible = false;
            this.toQuery();
          }
          this.$message.info(message);
        })
      },
      toAdd() {
        this.dialogPetFormVisible = true;
        this.petForm = {};
      },
      toEdit(row) {
        //根据主键ID查询宠物信息（带petDetail详情）
        /*this.$http.get("/pet/" + id).then(res => {
          this.petForm = res.data;
          this.dialogPetFormVisible = true;
        })*/
        this.petForm = Object.assign({},row);
        this.dialogPetFormVisible = true;
      },
      handleAvatarSuccess(res, file) {
        alert(res.data)
        this.petForm.resources = res.data;
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      shenghe(state) {
        this.form.state = state;
        this.$http.post('/pet/shenghe/', this.form).then(res => {
          let {msg, success} = res.data;
          if (success) {
            this.dialogFormVisible = false;
            this.toQuery();
          } else {
            this.$message.error(msg);
          }
        })
      },
      toQuery() {
        if (this.searchForm.betweenTime && this.searchForm.betweenTime.length > 0) {//判断参数非空（!= '' && != underfind && != NAN）
          this.searchForm.startTime = this.searchForm.betweenTime[0]
          this.searchForm.endTime = this.searchForm.betweenTime[1]
        } else {
          this.searchForm.startTime = null
          this.searchForm.endTime = null
        }
        this.$http.post("/pet/findPage", this.searchForm).then(res => {
          this.tableData = res.data.list;
          this.total = res.data.total;
        })
      },
      handleEdit(index, row) {
        console.log(index, row);
      },
      handleSizeChange(val) {
        this.searchForm.size = val;
        this.toQuery();
      },
      handleCurrentChange(val) {
        this.searchForm.page = val;
        this.toQuery();
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      }
    },
    mounted() {
      this.toQuery();
    }
  }
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
